# Copyright (C) 2005-2012, 2016, 2017 Canonical Ltd
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA

# A relatively simple Makefile to assist in building parts of brz. Mostly for
# building documentation, etc.


### Core Stuff ###

SHELL=bash
PYTHON?=python3
PYTHON3?=python3
BRZ_TARGET=release
PLUGIN_TARGET=plugin-release
PYTHON_BUILDFLAGS=
BRZ_PLUGIN_PATH=-site:-user

# Shorter replacement for $(sort $(wildcard <arg>)) as $(call sw,<arg>)
sw = $(sort $(wildcard $(1)))


.PHONY: all clean realclean extensions flake8 api-docs check-nodocs check

all: extensions

extensions:
	@echo "building extension modules."
	$(PYTHON) setup.py build_ext -i $(PYTHON_BUILDFLAGS)

check:: docs check-nodocs

check-nodocs: brz
	-$(RM) -f selftest.log
	echo `date` ": selftest starts" 1>&2
	set -o pipefail; BRZ_PLUGIN_PATH=$(BRZ_PLUGIN_PATH) \
	  ./brz selftest -Oselftest.timeout=120 --strict \
	  --subunit2 $(tests) | tee selftest.log | subunit2pyunit
	echo `date` ": selftest ends" 1>&2
	# An empty log file should catch errors in the $(PYTHON3)
	# command above (the '|' swallow any errors since 'make'
	# sees the 'tee' exit code for the whole line
	if [ ! -s selftest.log ] ; then exit 1 ; fi
	# Check that there were no errors reported.
	subunit-stats < selftest.log

check-ci: docs extensions brz
	# FIXME: Remove -Wignore::FutureWarning once
	# https://github.com/paramiko/paramiko/issues/713 is not a concern
	# anymore -- vila 2017-05-24
	set -o pipefail; \
	BRZ_PLUGIN_PATH=$(BRZ_PLUGIN_PATH) \
	  ./brz selftest -v --parallel=fork -Oselftest.timeout=120 --subunit2

brz:
	$(PYTHON) setup.py build_rust -i $(PYTHON_BUILDFLAGS)

# Run Python style checker (apt-get install flake8)
#
# Note that at present this gives many false warnings, because it doesn't
# know about identifiers loaded through lazy_import.
flake8:
	$(PYTHON) -m flake8 breezy

mypy:
	$(PYTHON) -m mypy breezy

clean:
	$(PYTHON) setup.py clean
	-find . -name "*.pyc" -o -name "*.pyo" -o -name "*.so" | xargs rm -f

realclean: clean
	# Remove files which are autogenerated but included by the tarball.
	rm -f breezy/*_pyx.c breezy/bzr/*_pyx.c
	rm -f breezy/bzr/_simple_set_pyx.h breezy/bzr/_simple_set_pyx_api.h

# build tags for emacs and vim
TAGS:
	ctags -R -e breezy

tags:
	ctags -R breezy

# these are treated as phony so they'll always be rebuilt - it's pretty quick
.PHONY: TAGS tags


### Documentation ###

docs: docs-sphinx

clean-docs: clean-sphinx

html-docs: html-sphinx


### Man-page Documentation ###

MAN_DEPENDENCIES = breezy/builtins.py \
	$(call sw,breezy/*.py) \
	$(call sw,breezy/*/*.py) \
	tools/generate_docs.py \
	$(call sw,$(addsuffix /*.txt, breezy/help_topics/en)) 

MAN_PAGES = man1/brz.1
man1/brz.1: $(MAN_DEPENDENCIES)
	mkdir -p $(dir $@)
	$(PYTHON) tools/generate_docs.py -o $@ man


### Sphinx-style Documentation ###

# Build the documentation. To keep the dependencies down to a minimum
# for distro packagers, we only build the html documentation by default.
# Sphinx 0.6 or later is preferred for the best rendering, though
# Sphinx 0.4 or later should work. See http://sphinx.pocoo.org/index.html
# for installation instructions.
docs-sphinx: html-sphinx

# Clean out generated documentation
clean-sphinx:
	$(MAKE) -C doc/en clean
	$(MAKE) -C doc/developers clean

SPHINX_DEPENDENCIES = \
        doc/en/release-notes/index.txt \
        doc/en/user-reference/index.txt \
	doc/developers/Makefile \
	doc/developers/make.bat

NEWS_FILES = $(call sw,doc/en/release-notes/brz-*.txt)

doc/en/user-reference/index.txt: $(MAN_DEPENDENCIES)
	LANGUAGE=C $(PYTHON) tools/generate_docs.py -o $@ rstx

doc/en/release-notes/index.txt: $(NEWS_FILES) tools/generate_release_notes.py
	$(PYTHON) tools/generate_release_notes.py $@ $(NEWS_FILES)

doc/%/Makefile: doc/en/Makefile
	$(PYTHON) -c "import shutil; shutil.copyfile('$<', '$@')"

doc/%/make.bat: doc/en/make.bat
	$(PYTHON) -c "import shutil; shutil.copyfile('$<', '$@')"

# Build the html docs using Sphinx.
html-sphinx: $(SPHINX_DEPENDENCIES)
	$(MAKE) -C doc/en html
	$(MAKE) -C doc/developers api html

# Build the PDF docs using Sphinx. This requires numerous LaTeX
# packages. See http://sphinx.pocoo.org/builders.html for details.
# Note: We don't currently build PDFs for the Russian docs because
# they require additional packages to be installed (to handle
# Russian hyphenation rules, etc.)
pdf-sphinx: $(SPHINX_DEPENDENCIES)
	$(MAKE) -C doc/en latex
	$(MAKE) -C doc/developers latex
	$(MAKE) -C doc/en/_build/latex all-pdf
	$(MAKE) -C doc/developers/_build/latex all-pdf

# Build the CHM (Windows Help) docs using Sphinx.
# Note: HtmlHelp Workshop needs to be used on the generated hhp files
# to generate the final chm files.
chm-sphinx: $(SPHINX_DEPENDENCIES)
	$(MAKE) -C doc/en htmlhelp
	$(MAKE) -C doc/developers htmlhelp


# Build the texinfo files using Sphinx.
texinfo-sphinx: $(SPHINX_DEPENDENCIES)
	$(MAKE) -C doc/en texinfo
	$(MAKE) -C doc/developers texinfo

### Documentation Website ###

# Where to build the website
DOC_WEBSITE_BUILD = build_doc_website

# Build and package docs into a website, complete with downloads.
doc-website: html-sphinx pdf-sphinx
	$(PYTHON) tools/package_docs.py doc/en $(DOC_WEBSITE_BUILD)
	$(PYTHON) tools/package_docs.py doc/developers $(DOC_WEBSITE_BUILD)


### Miscellaneous Documentation Targets ###

# build a png of our performance task list
# this is no longer built by default; you can build it if you want to look at it
doc/developers/performance.png: doc/developers/performance.dot
	@echo Generating $@
	@dot -Tpng $< -o$@ || echo "Dot not installed; skipping generation of $@"


### Windows Support ###

# make all the installers completely from scratch, using zc.buildout
# to fetch the dependencies
# These are files that need to be copied into the build location to boostrap
# the build process.
# Note that the path is relative to tools/win32
BUILDOUT_FILES = buildout.cfg \
	buildout-templates/bin/build-installer.bat.in \
	ostools.py bootstrap.py

installer-all:
	@echo Make all the installers from scratch
	@# Build everything in a separate directory, to avoid cluttering the WT
	$(PYTHON) tools/win32/ostools.py makedir build-win32
	@# cd to tools/win32 so that the relative paths are copied correctly
	cd tools/win32 && $(PYTHON) ostools.py copytree $(BUILDOUT_FILES) ../../build-win32
	@# There seems to be a bug in gf.release.brz, It doesn't correctly update
	@# existing release directories, so delete them manually before building
	@# It means things may be rebuilt that don't need to be, but at least
	@# it will be correct when they do.
	cd build-win32 && $(PYTHON) ostools.py remove release */release
	cd build-win32 && $(PYTHON) bootstrap.py
	cd build-win32 && bin/buildout
	cd build-win32 && bin/build-installer.bat $(BRZ_TARGET) $(PLUGIN_TARGET)


clean-installer-all:
	$(PYTHON) tools/win32/ostools.py remove build-win32

# make brz.exe for win32 with py2exe
exe:
	@echo *** Make brz.exe
	$(PYTHON) tools/win32/ostools.py remove breezy/*.pyd
	$(PYTHON) setup.py build_ext -i -f $(PYTHON_BUILDFLAGS)
	$(PYTHON) setup.py py2exe > py2exe.log
	$(PYTHON) tools/win32/ostools.py copytodir tools/win32/start_brz.bat win32_brz.exe
	$(PYTHON) tools/win32/ostools.py copytodir tools/win32/breezy.url win32_brz.exe

# win32 installer for brz.exe
installer: exe copy-docs
	@echo *** Make Windows installer
	$(PYTHON) tools/win32/run_script.py cog.py -d -o tools/win32/brz.iss tools/win32/brz.iss.cog
	iscc /Q tools/win32/brz.iss

py-inst-37: docs
	$(PYTHON37) setup.py bdist_wininst --install-script="brz-win32-bdist-postinstall.py" -d .

python-installer: py-inst-37

copy-docs: docs
	$(PYTHON) tools/win32/ostools.py copytodir README win32_brz.exe/doc
	$(PYTHON) tools/win32/ostools.py copydir doc/en/_build/html win32_brz.exe/doc
	$(PYTHON) tools/win32/ostools.py copydir doc/developers/_build/html win32_brz.exe/doc/developers

# clean on win32 all installer-related files and directories
clean-win32: clean-docs
	$(PYTHON) tools/win32/ostools.py remove build
	$(PYTHON) tools/win32/ostools.py remove win32_brz.exe
	$(PYTHON) tools/win32/ostools.py remove py2exe.log
	$(PYTHON) tools/win32/ostools.py remove tools/win32/brz.iss
	$(PYTHON) tools/win32/ostools.py remove brz-setup*.exe
	$(PYTHON) tools/win32/ostools.py remove brz-*win32.exe
	$(PYTHON) tools/win32/ostools.py remove dist


# i18n targets

.PHONY: update-pot po/brz.pot
update-pot: po/brz.pot

TRANSLATABLE_PYFILES:=$(shell find breezy -name '*.py' \
		| grep -v 'breezy/tests/' \
		| grep -v 'breezy/doc' \
		)

po/brz.pot: $(PYFILES) $(DOCFILES)
	./brz export-pot --include-duplicates > po/brz.pot
	echo $(TRANSLATABLE_PYFILES) | xargs \
	  xgettext --package-name "brz" \
	  --msgid-bugs-address "<breezy-vcs@groups.google.com>" \
	  --copyright-holder "Breezy Developers" \
	  --from-code UTF-8 --join --sort-by-file --add-comments=i18n: \
	  -d bzr -p po -o brz.pot


### Packaging Targets ###

.PHONY: dist check-dist-tarball

# build a distribution source tarball
dist:
	version=`./brz version --short` && \
	echo Building distribution of brz $$version && \
	expbasedir=`mktemp -t -d tmp_brz_dist.XXXXXXXXXX` && \
	expdir=$$expbasedir/brz-$$version && \
	tarball=$$PWD/../breezy-$$version.tar.gz && \
	$(MAKE) clean && \
	$(MAKE) && \
	$(PYTHON) setup.py sdist -d $$PWD/.. && \
	gpg --detach-sign --armor $$tarball && \
	rm -rf $$expbasedir

# run all tests in a previously built tarball
check-dist-tarball:
	tmpdir=`mktemp -t -d tmp_brz_check_dist.XXXXXXXXXX` && \
	version=`./brz version --short` && \
	tarball=$$PWD/../breezy-$$version.tar.gz && \
	tar Cxz $$tmpdir -f $$tarball && \
	$(MAKE) -C $$tmpdir/breezy-$$version check && \
	rm -rf $$tmpdir

reformat:
	isort breezy

check:: check-formatting

check-formatting:
	isort --check-only breezy

.testrepository:
	testr init

testr: .testrepository all
	testr run --parallel
